package lib.pacificapp.widget;

import lib.pacificapp.util.ScreenUtils;
import android.annotation.SuppressLint;
import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.nineoldandroids.view.ViewHelper; 
@SuppressLint("NewApi")
public class SlidingMenu extends HorizontalScrollView
{
	
	 public interface OnMenuListener{
		   void onOpen();
		   void onClose();
		 
	 }
	 
	 private OnMenuListener mOnMenuListener;
	 public void setOnMenuListener(OnMenuListener listener){
		 
		 mOnMenuListener=listener;
	 }
	private int mScreenWidth;
	 
	private int mMenuWidth;
	private int mHalfMenuWidth;

	private boolean isOpen;

	private boolean once;

	private ViewGroup mMenu;
	private ViewGroup mContent;
    private View slidingMenuLeftBar;
	public SlidingMenu(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
		initView();
	}
  private int mWidth=0;
private void initView(){
	   //设置为不能滑出边界(滑出边界的效果比较难看)
       setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
       setHorizontalFadingEdgeEnabled(false); 
       setHorizontalScrollBarEnabled(false);
       WindowManager wm = (WindowManager) getContext()
               .getSystemService(Context.WINDOW_SERVICE);
       mWidth = wm.getDefaultDisplay().getWidth(); 
   }
	public SlidingMenu(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		mScreenWidth = ScreenUtils.getScreenWidth(context);
           initView();
	}

	public SlidingMenu(Context context)
	{
		this(context, null, 0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		/**  */
		if (!once)
		{
			LinearLayout wrapper = (LinearLayout) getChildAt(0);
			 mMenu = (ViewGroup) wrapper.getChildAt(0);
			 mContent = (ViewGroup) wrapper.getChildAt(1);

			 mMenuWidth = (int) (mScreenWidth *0.9); // mMenuRightPadding;
			 mHalfMenuWidth = mMenuWidth / 2;
			 mMenu.getLayoutParams().width = mMenuWidth;
			 mContent.getLayoutParams().width = mScreenWidth;
			 slidingMenuLeftBar=this.findViewWithTag("slidingMenuLeftBar");
		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b)
	{
		super.onLayout(changed, l, t, r, b);
		if (changed&&!isLock())
		{ 
		    this.scrollTo(mMenuWidth, 0);
			once = true;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev)
	{
	  if(!isLock()){
		int action = ev.getAction();
		switch (action)
		{
		 	case MotionEvent.ACTION_UP:
			int scrollX = getScrollX();
			if (scrollX > mHalfMenuWidth)
			{
				 
				this.smoothScrollTo(mMenuWidth, 0);
				isOpen = false;
			} else
			{
				
				this.smoothScrollTo(0, 0);
				isOpen = true;
			}
			return true;
		}
	  }
		return super.onTouchEvent(ev);
	}

	 
	 public void openMenu()
	{
		if (isOpen) return;
		smoothScrollTo(0, 0);
		isOpen = true;
		
	}
 
	public void closeMenu()
	{
		if (isOpen)
		{
			this.smoothScrollTo(mMenuWidth, 0);
			isOpen = false;
		}
		isOpen=false;
	}

	 
	public void toggle()
	{
		if (isOpen)
		{
			closeMenu();
		} else
		{
			 openMenu();
		}
	}
    
	
	private boolean lock=false;
	@Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
		if(lock)return true;
		return super.onInterceptTouchEvent(e);
	}
   @Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt)
	{
		super.onScrollChanged(l, t, oldl, oldt);
	    if(isLock())return;
		 float scale = l * 1.0f / mMenuWidth;
		  
		 float rightScale = 0.8f + scale * 0.2f;  
		
	     ViewHelper.setScaleX(mMenu, 1f);
		 ViewHelper.setScaleY(mMenu, 1f);
		 
//		 if(slidingMenuLeftBar!=null){
//			RelativeLayout.LayoutParams ps= (RelativeLayout.LayoutParams)slidingMenuLeftBar.getLayoutParams();
//		      ps.setMargins(0, 0, (int)(rightScale*ps.width), 0);
//		      slidingMenuLeftBar.setLayoutParams(ps); ;
//		  }else{
//			  
//		 } 
		
		 int wid=Math.abs((l)/2);
//		 Log.d("testdd11",String.valueOf(wid));
//		 Log.d("testddcc11",String.valueOf(rightScale));
//		 if(lockValue==0){
//			 lockValue=wid;
//		 }
//		 if(isLock()){
//			 wid=lockValue;
//			 rightScale=1;
//		 }
//		 Log.d("testdd",String.valueOf(wid));
//		 Log.d("testddcc",String.valueOf(rightScale));
		 ViewHelper.setTranslationX(mMenu,wid);
	  	ViewHelper.setPivotX(mContent, 0);
	  	if(mHeight==0){
	  		mHeight= mContent.getHeight();
	  	}
		ViewHelper.setPivotY(mContent,mHeight);
		//ViewHelper.setScaleX(mContent, rightScale);
		ViewHelper.setScaleX(mContent, 1f);
		ViewHelper.setScaleY(mContent, 1f);
        if(mOnMenuListener!=null){
    	   if(l==0){
    		   mOnMenuListener.onOpen();
    	    }else { 
    	    	mOnMenuListener.onClose();
    	    }
       }
      }
 
	 public boolean isLock() {
	return lock;
}
public void setLock(boolean lock) {
	this.lock = lock;
}
	private int mHeight=0;
     private int lockValue=0;
}
